package com.weigee.ktv.v.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.l.d.app.library.app.FragActivity;
import com.l.d.app.library.eventbus.EventSend;
import com.l.d.app.library.util.ListUtil;
import com.l.d.app.library.util.ToastUtil;
import com.l.d.app.library.view.skinsupport.SkinCompatCircleImageView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.weigee.ktv.R;
import com.weigee.ktv.m.EventTag;
import com.weigee.ktv.m.observer.DrawerLayoutObserver;
import com.weigee.ktv.m.retrofit.ApiService;
import com.weigee.ktv.p.TaskDetailContract;
import com.weigee.ktv.p.frag.FirstContentPresenterImpl;
import com.weigee.ktv.v.activity.ActivityUtil;
import com.weigee.ktv.v.activity.StandardActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import skin.support.SkinCompatManager;
import skin.support.widget.SkinCompatImageView;
import skin.support.widget.SkinCompatTextView;

/**
 * Created by 林德 on 2017/4/17 9:07.
 * <p/>
 */
public class FirstContentFragment extends BaseFragment2 implements TaskDetailContract.FirstContentView
{
    private TaskDetailContract.FirstContentPresenter mPresenter;

    @BindView(R.id.t_refresh)
    TwinklingRefreshLayout m_tRefresh;// 刷新控件
    @BindView(R.id.tv_orders)
    SkinCompatTextView m_tvOrders;// 我的订单
    @BindView(R.id.civ_portrait)
    SkinCompatCircleImageView m_civPortrait;// 用户头像
    Unbinder unbinder;
    @BindView(R.id.iv_page_top)
    SkinCompatImageView ivPageTop;
    @BindView(R.id.iv_room_reservation)
    SkinCompatImageView ivRoomReservation;
    @BindView(R.id.iv_request_song)
    SkinCompatImageView ivRequestSong;
    @BindView(R.id.iv_supermarket)
    SkinCompatImageView ivSupermarket;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.first_frag_content, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        getPresenter().onViewCreated(savedInstanceState);
        ListUtil.initTwinklingRefresh(m_tRefresh, new RefreshListenerAdapter()
        {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout)
            {
                super.onRefresh(refreshLayout);
                getPresenter().onLoadListData(false);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout)
            {
                super.onLoadMore(refreshLayout);
                m_tRefresh.finishLoadmore();
            }
        });
    }

    @OnClick({R.id.civ_portrait, R.id.tv_orders, R.id.iv_room_reservation, R.id.iv_request_song, R.id.iv_supermarket})
    protected void onContentClick(View view)
    {
        final int viewId = view.getId();
        switch (viewId)
        {
            case R.id.civ_portrait:// 点击头像
                // 打开左侧侧滑菜单
                EventSend.with(EventTag.EVENT_MAIN_DRAWER_LAYOUT_CHANGE_STATE).msg(DrawerLayoutObserver.LEFT_OPEN).build();
                break;
            case R.id.tv_orders: // 我的订单
                ToastUtil.makeTest(view.getContext(), "我的订单");
                break;
            case R.id.iv_room_reservation:// 包房预定
                FragActivity.start(StandardActivity.class, view.getContext(), ActivityUtil.ROOM_RESERVATION);
                break;
            case R.id.iv_request_song:// 点歌
                ToastUtil.makeTest(view.getContext(), "点歌");
                SkinCompatManager.getInstance().restoreDefaultTheme();
                break;
            case R.id.iv_supermarket:// 超市
                FragActivity.start(StandardActivity.class, view.getContext(), ActivityUtil.SUPERMARKET_STORE);
                //        SkinCompatManager.getInstance().loadSkin("debugskin.skin");
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        unbinder.unbind();
    }

    @NonNull
    @Override
    public Context getMyContext()
    {
        return getActivity();
    }

    @Override
    public TaskDetailContract.FirstContentPresenter getPresenter()
    {
        if (mPresenter == null)
        {
            mPresenter = new FirstContentPresenterImpl(this);
        }
        return mPresenter;
    }

    @Override
    public void loadImage(String ivPageTop, String ivRoomReservation, String ivRequestSong, String ivSupermarket)
    {
        final Context context = getActivity();

        Glide.with(context).load(ApiService.BASE_IMG_URL + ivPageTop).asBitmap().into(this.ivPageTop);
        Glide.with(context).load(ApiService.BASE_IMG_URL + ivRoomReservation).asBitmap().into(this.ivRoomReservation);
        Glide.with(context).load(ApiService.BASE_IMG_URL + ivRequestSong).asBitmap().into(this.ivRequestSong);
        Glide.with(context).load(ApiService.BASE_IMG_URL + ivSupermarket).asBitmap().into(this.ivSupermarket);

        m_tRefresh.finishRefreshing();
    }
}
